﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Aden.Model.Net;
using Aden.Model.Purchase;
using Aden.Model.MenuOrder;
using Aden.BLL.Purchase;
using Aden.Util.Common;

namespace AdenWebAPI.Controllers
{
    public class PurchaseOrderController : BaseController
    {
        /// <summary>
        /// 保存采购订单
        /// </summary>
        /// <param name="token"></param>
        /// <param name="po"></param>
        /// <returns></returns>
        [HttpPost]
        public Response SavePurchaseOrder(string token, [FromBody]PurchaseOrderHead po)
        {
            Response response = new Response();
            if (string.IsNullOrEmpty(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                int result = PurchaseOrderHelper.SavePurchaseOrder(po);
                if (result == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = result;
                }
            }
            return response;
        }

        /// <summary>
        /// 读取采购订单
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetPurchaseOrderLine(string token, [FromBody]PurchaseOrderHead param)
        {
            Response response = new Response();
            if (string.IsNullOrEmpty(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                List<PurchaseOrderLine> lstPOL = PurchaseOrderHelper.GetPurchaseOrderLine(param);
                if (lstPOL == null || lstPOL.Count == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = lstPOL;
                }
            }
            return response;
        }

        /// <summary>
        /// 根据成本中心、日期导入采购单临时表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response ImportToPurchaseOrder(string token, [FromBody]MenuOrderHead param)
        {
            Response response = new Response();
            if (string.IsNullOrEmpty(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invild token";
            }
            else
            {
                int result = PurchaseOrderHelper.ImportToPurchaseOrder(param);
                if (result == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = result;
                }
            }
            return response;
        }

        [HttpPost]
        //dbName, costCenterCode, costCenterCode, requiredDate, directBuy
        public Response GetPurchaseitems(string token, [FromBody]string d)
        {
            Dictionary<string, dynamic> menu = d.JSDeserialize<Dictionary<string, dynamic>>();
            Response response = new Response();
            if (string.IsNullOrEmpty(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid token";
            }
            else
            {
                try
                {
                    var data = PurchaseOrderHelper.GetPurchaseItems(menu);
                    if (data == null)
                        throw new Exception("No data");

                    response.code = "200";
                    response.content = data;
                }
                catch(Exception e)
                {
                    response.code = "500";
                    response.message = e.Message;
                }
            }
            return response;
        }

        [HttpPost]
        public Response SavePOData(string token, [FromBody]MenuOrderPurchaseParam param)
        {
            Response response = new Response();
            if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid Token";
            }
            else
            {
                var data = PurchaseOrderHelper.SavePOData(param);
                if (data == 0)
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = data;
                }
            }
            return response;
        }

        /// <summary>
        /// 获取采购单数据 - PO.vue
        /// </summary>
        /// <param name="token"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetPOData(string token, [FromBody]POItemParam param)
        {
            Response response = new Response();
            if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
            {
                response.code = "404";
                response.message = "Invalid Token";
            }
            else
            {
                //MenuOrderPurchaseParam
                Dictionary<string, object> mopp = PurchaseOrderHelper.GetPOData(param);
                if (mopp == null) //&& mopp["PO"] == null && mopp["DeadLine"] == null && mopp["Remark"] == null
                {
                    response.code = "500";
                    response.message = "No Data";
                }
                else
                {
                    response.code = "200";
                    response.content = mopp;
                }
            }
            return response;
        }

        /// <summary>
        /// 获取采购单的供应商 - 日期列表 - PO日期（版本2）
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public Response GetPOSupplierDates(string token, [FromBody]MenuPOSupplierDates param)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                    throw new Exception( "Invalid Token");

                   var data = PurchaseOrderHelper.GetPOSupplierDates(param);
                if (data == null || !data.Any())
                    throw new Exception( "No Data");

                response.code = "200";
                response.content = data;
            }
            catch(Exception e)
            {
                response.code = "500";
                response.message = e.Message;
            }
            return response;
        }

    }
}